package cn.iocoder.yudao.module.reading.controller.app.community;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.comment.CommentCreateReqVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.comment.CommentRespVO;
import cn.iocoder.yudao.module.reading.controller.app.community.vo.common.LikeToggleRespVO;
import cn.iocoder.yudao.module.reading.service.community.CommunityCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 用户 APP - 社区评论 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "用户 APP - 社区评论")
@RestController
@RequestMapping("/reading/community/comments")
@Validated
@Slf4j
public class CommunityCommentController {

    @Resource
    private CommunityCommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    @PreAuthenticated
    public CommonResult<Long> createComment(@Valid @RequestBody CommentCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        Long commentId = commentService.createComment(createReqVO, userId);
        return success(commentId);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @PreAuthenticated
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        commentService.deleteComment(id, userId);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子的评论分页")
    public CommonResult<PageResult<CommentRespVO>> getCommentPage(
            @Parameter(description = "帖子ID", required = true) @RequestParam("postId") Long postId,
            @Valid PageParam pageParam) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        PageResult<CommentRespVO> pageResult = commentService.getCommentPage(postId, pageParam, userId);
        return success(pageResult);
    }

    @PostMapping("/{commentId}/like")
    @Operation(summary = "切换评论点赞状态")
    @PreAuthenticated
    public CommonResult<LikeToggleRespVO> toggleCommentLike(
            @Parameter(description = "评论ID", required = true) @PathVariable("commentId") Long commentId) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        LikeToggleRespVO result = commentService.toggleCommentLike(commentId, userId);
        return success(result);
    }

}
